package com.webwork.lims.transport.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.transport.dataobject.Zone;
import com.webwork.lims.transport.service.ZoneService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/baseInfo")
public class ZoneController extends SuperCtrl {

	@Autowired
	private ZoneService zoneSve;
	
	@RequestMapping(value = "/zones")
	@Override
	public String mainPage() {
		return "zones";
	}
	
	@RequestMapping(value = "queryZoneList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		Zone queryObj = new Zone();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, Zone.class);
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", zoneSve.count(queryObj));
		ret.put("rows", zoneSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value="loadZone")
	@ResponseBody
	public ResponseEntity<String> loadZone() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		List<Zone> zones = zoneSve.query(new Zone());
		List<Object> ret = new ArrayList<Object>();
		for (Zone o : zones) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getZoneCode());
			map.put("name", o.getCountry() + " - " + o.getProvince() + " - " + o.getCity());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getZone")
	@ResponseBody
	public ResponseEntity<String> getZone(@RequestParam("zoneCode") int zoneCode) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Zone zone = zoneSve.get(zoneCode);
		return new ResponseEntity<String>(new Gson().toJson(zone).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addZone")
	@ResponseBody
	public String addZone(@RequestParam("jsonObj") String jsonObj, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Zone zone = new Gson().fromJson(jsonObj, Zone.class);
		if (zoneSve.get(zone.getZoneCode()) != null) {
			return EXIST;
		}
		if (zoneSve.getByDelivery(zone.getDelivery()) != null) {
			return "USER";
		}
		
		zoneSve.save(zone);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editZone")
	@ResponseBody
	public String editZone(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Zone z = new Gson().fromJson(jsonObj, Zone.class);
		Zone zone = zoneSve.get(z.getZoneCode());
		if (zone == null) {
			return NOT_EXIST;
		}
		if (zoneSve.getByDelivery(z.getDelivery()) != null) {
			return "USER";
		}
		zone.setCountry(z.getCountry());
		zone.setProvince(z.getProvince());
		zone.setCity(z.getCity());
		zone.setDelivery(z.getDelivery());
		zoneSve.save(zone);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delZone")
	@ResponseBody
	public String delZone(@RequestParam("id") int id, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Zone zone = zoneSve.get(id);
		if (zone == null) {
			return FAIL;
		}
		zoneSve.delete(id);
		
		return SUCCESS;
	}
	
}
